package com.renjibo.teawithmilkgoods.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renjibo.teawithmilkgoods.Model.GoodsBean;
import com.renjibo.teawithmilkgoods.Model.ProductBean;
import com.renjibo.teawithmilkgoods.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 任继波
 * Created by Administrator on 2017/2/15.
 */

public class GoodsDetailsAdapter extends RecyclerView.Adapter<GoodsDetailsAdapter.HomeViewHolder> {

    private ProductBean productBean;
    private  Context context;
    public GoodsDetailsAdapter(ProductBean productBean, Context context){
       this.productBean = productBean;
        this.context=context;
    }
    @Override
    public GoodsDetailsAdapter.HomeViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {

        final View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.topic_recycle__item,parent,false);
        final GoodsDetailsAdapter.HomeViewHolder viewHolder=new GoodsDetailsAdapter.HomeViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(productBean==null){
            return;
        }
        ProductBean.DataBean.ProductsBean productsBean = productBean.getData().getProducts().get(position);
        Picasso.with(context).load(productsBean.getImage_small()).into(holder.topic_recycle_imageView);
        holder.topic_recycle_text_present.setText(productsBean.getShort_description());
        if(productsBean.getFeatured_price()==null||productsBean.getFeatured_price().length()<=0){
            holder.topic_recycle_text_new_price.setText("￥"+productsBean.getPrice());
            holder.topic_recycle_text_old_price.setVisibility(View.INVISIBLE);
            holder.topic_recycle_text_price.setVisibility(View.INVISIBLE);
            holder.topic_recycle_text_name.setText(productsBean.getName());

//            holder.home_recycle_right_imageView
        }else{
            holder.topic_recycle_text_old_price.setVisibility(View.VISIBLE);
            holder.topic_recycle_text_price.setVisibility(View.VISIBLE);
            holder.topic_recycle_text_old_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
            holder.topic_recycle_text_name.setText(productsBean.getName());
            holder.topic_recycle_text_new_price.setText("￥"+productsBean.getFeatured_price()+"");
            holder.topic_recycle_text_old_price.setText("￥"+productsBean.getPrice()+"");

        float v = Float.valueOf(productsBean.getPrice()) - Float.valueOf(productsBean.getFeatured_price());
        holder.topic_recycle_text_price.setText("立减￥"+v+"元");
        }
    }

    @Override
    public int getItemCount() {
        if(productBean!=null) {
            return productBean.getData().getProducts().size();
        }
        return 0;
    }

    class HomeViewHolder extends RecyclerView.ViewHolder{
        TextView topic_recycle_text_name;
        TextView topic_recycle_text_price;
        ImageView topic_recycle_imageView;
        TextView topic_recycle_text_old_price;
        TextView topic_recycle_text_new_price;
        TextView topic_recycle_text_present;
        public HomeViewHolder(View itemView) {
            super(itemView);
            topic_recycle_text_old_price= (TextView) itemView.findViewById(R.id.topic_recycle_text_old_price);
            topic_recycle_text_new_price= (TextView) itemView.findViewById(R.id.topic_recycle_text_new_price);
            topic_recycle_text_name= (TextView) itemView.findViewById(R.id.topic_recycle_text_name);
            topic_recycle_text_price= (TextView) itemView.findViewById(R.id.topic_recycle_text_price);
            topic_recycle_imageView= (ImageView) itemView.findViewById(R.id.topic_recycle_imageView);
            topic_recycle_text_present= (TextView) itemView.findViewById(R.id.topic_recycle_text_present);
        }
    }
}
